#include "information.h"
#include <GL/glut.h>
#include <stdio.h>
#include <iostream>


using namespace std;
 
Information::Information()
{
  _visibility = false;
  _texts = new list<Text*>();
  _material->setEmissive(0.5,0.0,0.8,1.0);
}


Information::~Information()
{
  if(this->_texts) 
     delete(_texts);
}



/*Getters-Setters*/
void Information::setText(Text* t)
{
  if(!this->_texts) _texts = new list<Text*>();
  this->_texts->push_back(t);
}
  
void Information::setVisibility(bool b)
{
  this->_visibility = b;  
}




/*Dessin des textes*/
void Information::_draw()
{
  if(_visibility)
  {
    for(list<Text*>::iterator it = _texts->begin();it != _texts->end();it++)
    {
      this->_drawTracet(*it);
      (*it)->_draw();
    }
  }
}



/*Dessin du cadre*/
void drawSimpleRectangle(float x,float y,float z,float len);
void drawRoundedRectangle(float x,float y,float z,int len);

void Information::_drawTracet(Text* t)
{
  float x,y,z;
  int len;

  //cout<<"voici le text : "<<*(t->getText())<<endl;

  t->getPosition(x,y,z);
  len = (t->getText())->size();

  //cout<<x<<" "<<y<<" "<<z<<" "<<len<<endl;
  //drawRoundedRectangle(x,y,z,len);
  
  drawSimpleRectangle(x,y,z,len);
}


void drawSimpleRectangle(float x,float y,float z,float len)
{
  double hauteurCadre = 2.0;
  double largeurCadre = len * 1.5;
  //rectangle
  glColor3f(0.0,0.0,0.0);
  glBegin(GL_POLYGON);
   glVertex2d(x,y);
   glVertex2d(x+largeurCadre,y);
   glVertex2d(x+largeurCadre,y+hauteurCadre);
   glVertex2d(x,y+hauteurCadre);
  glEnd();

  //triangle
  glColor3f(0.0,0.0,0.0);
  glBegin(GL_POLYGON);
   glVertex2d(x+9.0*largeurCadre/20.0,y);
   glVertex2d(x+largeurCadre/2.0,y-hauteurCadre/3.0);
   glVertex2d(x+11.0*largeurCadre/20.0,y);
  glEnd();

}

void drawRoundedRectangle(float x ,float y,float z)
{
  glColor3f(0.0, 0.0, 0.5);

  int steps = 20;

  double sc = 5.0;

  double I1x = 5.0;
  double I1y = 5.0;

  double delta = ((M_PI/2.0) / (double)steps);
  double lastX = I1x - sc;
  double lastY = I1y;			

  for( double w = delta, i = 1; i <= steps; w += delta, ++i ) 
  {
    double x = -cos(w);
    double y = -sin(w);
    double x1 = I1x + (x * sc);
    double y1 = I1y + (y * sc);


  glBegin(GL_TRIANGLES);
   glVertex2d(lastX, lastY);
   glVertex2d(x1, y1);
   glVertex2d(I1x, I1y);
  glEnd();

  lastX = x1;
  lastY = y1;
  }
}

